import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router);

/* Layout */
import Layout from '@/layout'

// 常量路由:不管用户是什么角色,都可以看见的
export const constantRoutes = [
    {
        path: '/login',
        component: () => import('@/views/login/index'),
        hidden: true
    },

    {
        path: '/404',
        component: () => import('@/views/404'),
        hidden: true
    },

    {
        path: '/',
        component: Layout,
        redirect: '/dashboard',
        children: [{
            path: 'dashboard',
            name: 'Dashboard',
            component: () => import('@/views/dashboard/index'),
            meta: { title: '首页', icon: 'dashboard' }
        }]
    },

];

// 异步路由:不同的用户,需要过滤筛选出的路由称之为异步路由
export const asyncRoutes = [
    {
        path: '/product',
        component: Layout,
        name: 'Product',
        meta: { title: '商品管理', icon: 'el-icon-goods' },
        children: [
            {
                path: '/tradeMark',
                name: 'Trademark',
                component: () => import('@/views/product/TradeMark'),
                meta: { title: '分类管理'},
            },
            {
                path: '/attr',
                name: 'Attr',
                component: () => import('@/views/product/Attr'),
                meta: { title: '属性管理'},
            },
            {
                path: '/sup',
                name: 'Spu',
                component: () => import('@/views/product/Sup'),
                meta: { title: 'Sup管理'},
            },
            {
                path: '/sub',
                name: 'Sku',
                component: () => import('@/views/product/Sub'),
                meta: { title: 'Sub管理'},
            },

        ]
    },

    {
        path: '/right',
        component: Layout,
        name: 'Role',
        meta: { title: '权限管理', icon: 'el-icon-lock' },
        children: [
            {
                path: '/userRight',
                name: 'User',
                component: () => import('@/views/rights/UsersManage'),
                meta: { title: '用户管理'},
            },
            {
                path: '/roleRight',
                name: 'RoleAuth',
                component: () => import('@/views/rights/RolesManage'),
                meta: { title: '角色管理'},
            },
            {
                path: '/menuRight',
                name: 'Order',
                component: () => import('@/views/rights/MenuManage'),
                meta: { title: '菜单管理'},
            },

        ]
    },
];

// 任意路由:
export const anyRoutes = [
    // 404 page must be placed at the end !!!
    { path: '*', redirect: '/404', hidden: true }
];

const createRouter = () => new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    // 给路由器添加新的路由
    routes: constantRoutes
});

const router = createRouter();

export function resetRouter() {
    const newRouter = createRouter();
    router.matcher = newRouter.matcher // reset router
}

export default router
